﻿using HomagChina.Basic.CCT.Db.CCT_Db;
using Serilog.Core;
using System;
using System.Linq;

namespace Proxy
{
    public class ModifyInfoHandle
    {
        private CommonHandle Handle_Common;

        public ModifyInfoHandle()
        {
            Handle_Common = new CommonHandle();
        }

        public bool ModifyMachineConfig(T_MachineInfo t, int Id)
        {
            bool result = false;
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    var Info = Db.T_MachineInfo.FirstOrDefault(x => x.Id == Id);
                    Info.MachineNo = t.MachineNo;
                    Info.IP = t.IP;
                    Info.AvailableChannels = t.AvailableChannels;
                    Info.ModificationDate = DateTime.Now;
                    result = Db.SaveChanges() > 0;
                }
                return result;
            }
            catch (Exception ex)
            {
                SerilogHandle.Logger_Modify.Error(ex.Message);
                return result;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="t"></param>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool ModifyDirectionInfo(T_DirectionConfig t, int Id)
        {
            bool result = false;
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    var DirectionConfigInfo = Db.T_DirectionConfig.FirstOrDefault(x => x.Id.Equals(Id));
                    DirectionConfigInfo.SourcePos = t.SourcePos;
                    DirectionConfigInfo.TargetPos = t.TargetPos;
                    DirectionConfigInfo.Direction = t.Direction;
                    return Db.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                SerilogHandle.Logger_Modify.Error(ex.Message);
                return result;
            }
        }

        /// <summary>
        /// 更新传输状态
        /// </summary>
        /// <param name="Reference"></param>
        /// <param name="State"></param>
        public void ModifyTransState(string Reference, string Location = "10")
        {
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    T_TransOnlineRecords t_TransOnlineRecords = Db.T_TransOnlineRecords.FirstOrDefault(s => s.Reference.Equals(Reference));
                    if (t_TransOnlineRecords != null)
                    {
                        if (!t_TransOnlineRecords.Location.Equals("100"))
                        {
                            t_TransOnlineRecords.State = (int)EnumOfOrderState.Plc完成任务;
                            Db.SaveChanges();
                        }
                        else
                        {
                            Db.T_TransOnlineRecords.Remove(t_TransOnlineRecords);
                            Db.SaveChanges();
                            return;
                        }
                    }
                    else
                    {
                        SerilogHandle.Logger_System.Error("Reference:" + Reference + ":数据更新失败！");
                    }
                }
                using (Db_CCT Db = new Db_CCT())
                {
                    T_TransOnlineRecords t_TransOnlineRecords = Db.T_TransOnlineRecords.FirstOrDefault(s => s.Reference.Equals(Reference));
                    if (t_TransOnlineRecords != null)
                    {
                        T_DirectionConfig t_DirectionConfig = Db.T_DirectionConfig.FirstOrDefault(s => s.SourcePos.Equals(t_TransOnlineRecords.Location) && s.TargetPos.Equals(t_TransOnlineRecords.NextPosition));
                        t_TransOnlineRecords.Direction = t_DirectionConfig.Direction;
                        Db.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                SerilogHandle.Logger_Modify.Error(ex.Message);
            }
        }

        /// <summary>
        /// 更新传输状态
        /// </summary>
        /// <param name="Reference"></param>
        /// <param name="State"></param>
        public void ModifyTransState(string Reference)
        {
            try
            {
                var List_Ref = Reference.Split('@').ToList();
                var RefCount = List_Ref.Count();
                using (Db_CCT Db = new Db_CCT())
                {
                    for (int i = 0; i < RefCount; i++)
                    {
                        var currentRef = List_Ref[i];
                        if (currentRef == "")
                        {
                            continue;
                        }
                        T_TransOnlineRecords t_TransOnlineRecords = Db.T_TransOnlineRecords.FirstOrDefault(s => s.Reference == currentRef);

                        if (t_TransOnlineRecords == null)
                        {
                            return;
                        }

                        var Location = t_TransOnlineRecords.NextPosition;
                        if (Location.Equals("OFF"))
                        {
                            Db.T_TransOnlineRecords.Remove(t_TransOnlineRecords);
                            Db.SaveChanges();
                            return;
                        }
                        Globle.RouteInfo = Globle.RoutesInfo.Where(s => s.RouteId.Equals(t_TransOnlineRecords.RouteId)).ToList();

                        var detail = Globle.RouteInfo.FirstOrDefault(s => s.SourcePosition.Contains(t_TransOnlineRecords.NextPosition));

                        if (detail == null)
                        {
                            //当前位置不存在目标路径内
                            return;
                        }
                        t_TransOnlineRecords.Location = detail.SourcePosition;
                        t_TransOnlineRecords.NextPosition = detail.TargetPosition;
                        t_TransOnlineRecords.Direction = detail.Direction;
                        Db.SaveChanges();
                    }

                }
            }
            catch (Exception ex)
            {
                SerilogHandle.Logger_Modify.Error(ex.Message);
            }
        }


        /// <summary>
        /// 更新路径
        /// </summary>
        /// <param name="Reference"></param>
        /// <param name="State"></param>
        public int ModifyTransRouteId(string Reference, int RouteId, Logger log)
        {
            int Direction = 10;
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    T_TransOnlineRecords t_TransOnlineRecords = Db.T_TransOnlineRecords.FirstOrDefault(s => s.Reference.Equals(Reference));

                    t_TransOnlineRecords.RouteId = RouteId;

                    T_RouteConfig t_RouteConfig = Db.T_RouteConfig.FirstOrDefault(s => s.Id == RouteId);

                    var Route = t_RouteConfig.Route.Split('-').ToArray();

                    var Location = t_TransOnlineRecords.Location;

                    var NextLocation = Route[Array.IndexOf(Route, Route.FirstOrDefault(x => x.Equals(Location))) + 1];

                    t_TransOnlineRecords.NextPosition = NextLocation;

                    Direction = (int)Db.T_DirectionConfig.FirstOrDefault(s => s.SourcePos.Equals(Location)
                                                                                        && s.TargetPos.Equals(NextLocation)).Direction;

                    T_BoardInfo t_BoardInfo = new T_BoardInfo();

                    Reference = Reference.Split('-')[0];
                    t_BoardInfo = Db.T_BoardInfo.FirstOrDefault(s => s.Reference.Equals(Reference));

                    if (Direction.Equals(40) && t_BoardInfo.Width > 620)
                    {
                        Direction = 60;
                    }

                    t_TransOnlineRecords.Direction = Direction;
                    Db.SaveChanges();
                }
                log.Information("Reference :" + Reference + "-> 改变路径 => " + RouteId + " , Direction -> " + Direction);
                return Direction;
            }
            catch (Exception ex)
            {
                SerilogHandle.Logger_Modify.Error(ex.Message);
                return Direction;
            }
        }


        /// <summary>
        /// 更新路径
        /// </summary>
        /// <param name="Reference"></param>
        /// <param name="State"></param>
        public int ModifyTransRouteId(string Reference, int RouteId, Logger log, bool Width_Judgement = false)
        {
            int Direction = 10;
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    T_TransOnlineRecords t_TransOnlineRecords = Db.T_TransOnlineRecords.FirstOrDefault(s => s.Reference.Equals(Reference));

                    if (t_TransOnlineRecords == null)
                    {
                        return Direction = -1;
                    }
                    t_TransOnlineRecords.RouteId = RouteId;

                    Globle.RouteInfo = Globle.RoutesInfo.Where(s => s.RouteId.Equals(RouteId)).ToList();

                    var detail = Globle.RouteInfo.FirstOrDefault(s => s.SourcePosition.Contains(t_TransOnlineRecords.Location));

                    if (detail == null)
                    {
                        //当前位置不存在目标路径内
                        return 0;
                    }
                    t_TransOnlineRecords.NextPosition = detail.TargetPosition;

                    Direction = detail.Direction;

                    T_BoardInfo t_BoardInfo = new T_BoardInfo();

                    Reference = Reference.Split('-')[0];
                    t_BoardInfo = Db.T_BoardInfo.FirstOrDefault(s => s.Reference.Equals(Reference));

                    switch (t_BoardInfo.Width > 670 && Width_Judgement == true)
                    {
                        case true:
                            Direction = Direction == 40 ? 60 : Direction == 50 ? 70 : Direction == 10 ? 60 : 10;
                            break;
                    }

                    t_TransOnlineRecords.Direction = Direction;
                    Db.SaveChanges();
                }
                log.Information("Reference :" + Reference + "-> 改变路径 => " + RouteId + " , Direction -> " + Direction);
                return Direction;
            }
            catch (Exception ex)
            {
                SerilogHandle.Logger_Modify.Error(ex.Message);
                return Direction;
            }
        }

        /// <summary>
        /// 更新线上板件信息
        /// </summary>
        /// <param name="Location"></param>
        public bool ModifyBoardInfo(float Width, float Length, float Thickness, string Zeroline, string Reference)
        {
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    T_BoardInfo t_BoardInfo = Db.T_BoardInfo.Where(s => s.Reference == Reference).FirstOrDefault();
                    if (t_BoardInfo != null)
                    {
                        t_BoardInfo.Length = Length;
                        t_BoardInfo.Width = Width;
                        t_BoardInfo.Thickness = Thickness;
                        t_BoardInfo.Feature = Zeroline == "Both" ? "B" : Zeroline == "Left" ? "L" : "R";
                    }
                    return Db.SaveChanges() >= 0;
                }
            }
            catch (Exception ex)
            {
                SerilogHandle.Logger_Modify.Error(ex.Message);
                return false;
            }
        }
    }
}